package com.bi.onlineEducation.service;

import com.bi.onlineEducation.dao.CourseTypeDao;
import com.bi.onlineEducation.domain.CourseTypePo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: chenYuTao
 * @Time: 2022/1/19 23:56
 * @Description:
 **/
@Service
public class CourseTypeService {
    @Resource
    private CourseTypeDao courseTypeDao;

    public List<CourseTypePo> findAll() {
        return courseTypeDao.findAll();
    }

    public CourseTypePo findById(Integer id) {
        return courseTypeDao.findById(id).get();
    }

    public CourseTypePo save(CourseTypePo courseTypePo) {
        return courseTypeDao.save(courseTypePo);
    }

    public Boolean deleteById(Integer id) {
        courseTypeDao.deleteById(id);
        return true;
    }

    @Transactional
    public Boolean deleteByIds(List<Integer> ids) {
        for (Integer id : ids) {
            courseTypeDao.deleteById(id);
        }
        return true;
    }

    /**
     * 课程类型搜索所有
     *
     * @param ctName
     * @param realName
     * @return
     */
    public List<CourseTypePo> search(String ctName, String realName) {
        List<CourseTypePo> all = courseTypeDao.findAll();
        ArrayList<CourseTypePo> resultList = new ArrayList<>();
        ArrayList<CourseTypePo> ctNameList = new ArrayList<>();
        ArrayList<CourseTypePo> realNameList = new ArrayList<>();
        for (CourseTypePo courseTypePo : all) {
            if (StringUtils.isNotBlank(ctName) && courseTypePo.getCtName().indexOf(ctName) > -1) {
                ctNameList.add(courseTypePo);
            }
            if (StringUtils.isNotBlank(realName) && courseTypePo.getLogin().getRealName().indexOf(realName) > -1) {
                realNameList.add(courseTypePo);
            }
        }
        if (StringUtils.isBlank(ctName)) ctNameList = (ArrayList<CourseTypePo>) all;
        if (StringUtils.isBlank(realName)) realNameList = (ArrayList<CourseTypePo>) all;
        for (CourseTypePo ctNameListVo : ctNameList) {
            for (CourseTypePo realNameVo : realNameList) {
                if (ctNameListVo.getCtId() != realNameVo.getCtId()) continue;
                resultList.add(realNameVo);
            }
        }
        return resultList;
    }
}
